package com.yun.platform.sys.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.platform.config.dto.RData;
import com.yun.platform.config.dto.Yun;
import com.yun.platform.excption.BizException;
import com.yun.platform.sys.entity.User;
import com.yun.platform.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author:laich  Date:2021/9/10
 *
 * @Desciprtion:  用户接口
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(value = "用户相关接口", tags = {"用户操作接口"})
public class UserController extends BaseController{

    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private UserService userService;


    @PostMapping("/updateUser")
    @ApiOperation("用户修改资料接口")
    public RData updateUser(@RequestParam("id")int id, @RequestParam("name")String name){
        User user = new User();
        if("".equals(id)){
            throw new BizException("id信息不能为空");
        }
        if(name == null || "".equals(name)){
            throw new BizException("修改信息不能为空");
        }
        user.setId(id);
        user.setUserName(name);
        User map1 = userService.updateUser(user);
        return RData.success(map1);
    }
    @PostMapping("/getUser")
    @ApiOperation("获得用户资料接口")
    public RData getUser(@RequestParam("phone")String phone){

        User map1 = userService.findLogin(phone);
        if(map1 !=null){
            throw new BizException("sdfsdfs");
        }
        log.info("data:{}", JSON.toJSONString(RData.success(map1),true));
        return RData.success(map1);
    }

    @PostMapping("/listUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String")
    })
    public RData listUser(@RequestParam("page")Integer page){
        IPage<User> userPage = new Page<User>(page, Yun.pageSize);//参数一是当前页，参数二是每页个数
        IPage<User> listUser = userService.page(userPage, new QueryWrapper());

        log.info("data:{}", JSON.toJSONString(RData.success(listUser),true));
        return RData.success(listUser);
    }
}
